/*
 *说明：用户已选择的授权资源脚本
 *
 *@author liuxun 2012-03-26
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'节点名称',field:'NODE_DECODE',align:'center',width:60},
            
            {title:'资源组',field:'RSC_GRP_NAME',align:'center',width:80},

            {title:'资源',field:'RSC_NAME',align:'center',width:100},
            
            {title:'账号',field:'RSC_ACC',align:'center',width:100},

            {title:'协议',field:'PRO_NAME',align:'center',width:60},
            
            {title:'端口',field:'PORT',align:'center',width:40},
            
            {title:'运维目的',field:'OPERATION_TYPE_DECODE',align:'center',width:60},
            
            {title:'开始时间',field:'START_TIME',align:'center',width:120},
            
            {title:'有效期(分钟)',field:'AVAILABLE_LENGTH',align:'center',width:80,
            	formatter:function(value, rec){
            		return value == null ? '': value;
            	}
            },

            {title:'状态',field:'ACL_STATUS',align:'center',width:40,
            	formatter:function(value,rec){
            		return value == '01' ? "激活" : "<font color='red'>失效</font>";
			    }
            },
            
            {title:'策略名称',field:'CMD_POLICY_NAME',align:'center',width:80},
            
			{title:'操作',field:'operation',align:'center',width:110,
	        	formatter:function(value,rec){
	        		var s = '<a style="cursor:pointer; color:blue" onclick=\'showCmdPolicy("' + rec.ACL_ID + '", "'+rec.POLICY_ID+'")\'>选择策略</a>';
	        		 s += '&nbsp;&nbsp;<a style="cursor:pointer; color:blue" onclick=\'clearPolicy("' + rec.ACL_ID + '", "'+ rec.POLICY_ID+'")\'>撤销策略</a>';
	        		
	        		 //s += '&nbsp;&nbsp;<a style="cursor:pointer; color:blue" onclick=\'showInfo("' + rec.ACL_ID + '")\'>明细</a>';
	        	    return s;
		   		}
		    }
        ]],
		pagination:false,
		singleSelect: false
	});

	queryOperation();

	$('#reloadBtn').click(refreshWin);
	$("#addBtn").click(addOperation);
	$('#activateBtn').click(activateOperation);
	$('#deactivateBtn').click(deactivateOperation);
	$("#deleteBtn").click(deleteOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'D_ACL!queryUsrSelectedAuthRscList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

// 选择策略命令
function showCmdPolicy(acl_id, policy_id) {
	if(!isEmpty(policy_id) && policy_id != 'null'){   
		showInfoWin('此资源已挂接命令策略！');
		return;
	}

	var action = 'D_ACL!showCmdPolicySelect.do';
	var row = showModalWin(action,'A2');
	
	if (row) {
		var cmd_policy_id = row.cmd_policy_id;
		var params = $ .param({'model.policy_id' : cmd_policy_id, 'model.acl_id' : acl_id}, true);
		var options = { 
				url: 'D_ACL!doUpdateByCmdPolicy.do?' + params,
				type: 'post',
				success: function(data){
					afterOK();
				}
		};

		opeForm.ajaxSubmit(options);
	}
}

//撤销命令策略
function clearPolicy(key, policy_id) {
	if(isEmpty(policy_id) || policy_id == 'null'){
		showInfoWin('资源未挂接命令策略！');
		return;
	}
	
	var params = {'model.key': key};

	$ .messager.confirm('提示信息', '您确定要撤销选定的策略吗?', function(result){
		if(result){
			$ .ajax({
				url: 'D_ACL!doClearPolicy.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});	
}

//查看明细
function showInfo(key) {
	var url = 'D_ACL!showInfo.do?model.key=' + key;
	openWin(url,'pop'); 
}

//新增授权资源操作
function addOperation() {
	
	var usr_id = $('#auth_obj_id').val();
	
	if(usr_id == 'undefined'){   
		showInfoWin('请选择用户节点！');
		return;
	}
	
	var url = 'D_ACL!showUsrNotSelectedRsc.do?firstQuery=true&model.auth_obj_id=' + usr_id;
	openWin(url, 'pop', 'A0', '1150', '755');
}

//激活操作 
function activateOperation() {
	var usr_id  = $('#auth_obj_id').val();
	
	if(usr_id == 'undefined'){   
		showInfoWin('请选择用户节点！');
		return ;
	}
	
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ACL_ID);
	}
	var params = $ .param({'id' : ids}, true);
	
	var url = 'D_ACL!showReActivate.do?params=' + ids;
	openWin(url, 'pop', 'A0', '1050', '750');
}

//失效操作
function deactivateOperation() {	
	var usr_id  = $('#auth_obj_id').val();
	if(usr_id == 'undefined'){   
		showInfoWin('请选择用户节点！');
		return;
	}
	
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		if (rows[i].ACL_STATUS == '02') {
			showInfoWin('选择的记录已经存在失效的记录!');
			return;
		}
		ids.push(rows[i].ACL_ID);
	}
	var params = $ .param({'id' : ids}, true);
	
	$.messager.confirm('提示信息', '您确定要失效选择的用户组授权资源信息吗?', function(result) {
		if (result) {
			showSystemProccessing();
			$.ajax( {
				url : 'D_ACL!doDeactivate.do',
				data : params,
				type : 'post',
				success : function(data) {
					removeSystemProccessing();
					showInfoWin("操作成功！");
					afterOK();
				}
			});
		}
	});
}

// 删除操作
function deleteOperation() {
	var usr_id = $('#auth_obj_id').val();
	if(usr_id == 'undefined'){   
		showInfoWin('请选择用户节点！');
		return;
	}
	
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ACL_ID);
	}
	var params = $ .param({'id' : ids}, true);
	
	$.messager.confirm('提示信息', '您确定要删除选择的用户组授权资源信息吗?', function(result) {
		if (result) {
			showSystemProccessing();
			$.ajax( {
				url : 'D_ACL!doDelete.do',
				data : params,
				type : 'post',
				success : function(data) {
					removeSystemProccessing();
					showInfoWin("操作成功！");
					afterOK();
				}
			});
		}
	});
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}